using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using Persistance;
using Persistance.Model;
using Lib; 

namespace PersistanceDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Persistor.IsConfigurationSet == false)
            {
                string SQLconnectionString = "";
                Persistor.Configure(SQLconnectionString, typeof(ApplicationLog), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Items), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Categories), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Locations), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Users), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Photos), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Requests), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(Status), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(ItemType), Persistor.DatabaseType.MSSQL2005);
                Persistor.Configure(SQLconnectionString, typeof(LogEntryType), Persistor.DatabaseType.MSSQL2005);
            }

            bool quit = false;
            while (!quit)
            {
                write(@"o. Output Schema to screen and save to file.");
                write(@"q. Quit");

                try
                {
                    string choice = read();
                    switch (choice)
                    {
                        case "o":
                            outputSchemaToFile();
                            break;

                        case "q":
                            quit = true;
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    throw;
                }
            }
        }

        private static void outputSchemaToFile()
        {

            string schema = Persistor.OutputSchema();
            Console.WriteLine(schema);

            TextWriter tw = new StreamWriter(@"C:\schema.sql", false);

            // write a line of text to the file

            tw.Write(schema);
            // close the stream
            tw.Close();
        }

        private static string read()
        {
            return read("");
        }

        private static string read(string prompt)
        {
            System.Console.Write(prompt + "> ");
            return System.Console.ReadLine();
        }

        private static void write(String theLine)
        {
            System.Console.Out.WriteLine(theLine);
        }

    }
}